#include "Sprite.h"

CSprite::CSprite()
{
	m_x = 0;
	m_y = 0;
	m_State			= 0;
	m_pNext			= 0;
	m_VelocityX		= 0;
	m_VelocityY		= 0;
	m_SurfaceWidth	= 0;
	m_SurfaceHeight	= 0;
	m_SpriteWidth	= 0;
	m_SpriteHeight	= 0;
	m_NumSprites	= 0;

	m_pSpriteSurface	= 0;
	m_pActiveSurface	= 0;

	m_bInitialized		= FALSE;
	m_bTransparent		= TRUE;
	
	if (zen.m_Mode == D2)
		m_ColorKey	= D3DCOLOR_ARGB(0, 255, 0, 255);	
	else
		m_ColorKey	= D3DCOLOR_ARGB(0xff, 255, 0, 255);	
}

CSprite::~CSprite()
{
	if (m_pSpriteSurface)
	{
		m_pSpriteSurface->Release();
		m_pSpriteSurface = NULL;
	}

	if (m_pActiveSurface)
	{
		m_pActiveSurface->Release();
		m_pActiveSurface = NULL;
	}
}

//Member-functions:
HRESULT CSprite::Initialize(char * strPathName, int SpriteWidth, int SpriteHeight, int NumSprites)
{
	HRESULT			res = 0;
	D3DSURFACE_DESC	d3dsd;

	if (m_pSpriteSurface)
	{
		m_pSpriteSurface->Release();
		m_pSpriteSurface = 0;
	}

	if (m_pActiveSurface)
	{
		m_pActiveSurface->Release();
		m_pActiveSurface = 0;
	}
	
	NumSprites--;

	zen.LoadBitmapToSurface(strPathName, &m_pSpriteSurface, zen.m_pDevice);
	if (FAILED(res))
		return E_FAIL;

	m_SpriteWidth  = SpriteWidth;
	m_SpriteHeight = SpriteHeight;
	m_NumSprites   = NumSprites;
	
	m_pSpriteSurface->GetDesc(&d3dsd);

	m_SurfaceWidth  = d3dsd.Width;
	m_SurfaceHeight = d3dsd.Height;

	res = zen.m_pDevice->CreateImageSurface(m_SpriteWidth, m_SpriteHeight, D3DFMT_X8R8G8B8,
										&m_pActiveSurface);
	if (FAILED(res))
	{
		Shutdown();
		return E_FAIL;
	}

	RECT  SpriteRect  = {0, 0, SpriteWidth, SpriteHeight};
	POINT SpritePoint = {0, 0};

	zen.m_pDevice->CopyRects(m_pSpriteSurface, &SpriteRect, 1, m_pActiveSurface, &SpritePoint);

	m_bInitialized = TRUE;

	return S_OK;
}

HRESULT CSprite::Initialize(LPDIRECT3DSURFACE8 pSourceSurface, int SpriteWidth, int SpriteHeight, int NumSprites)
{
	HRESULT			res = 0;
	D3DSURFACE_DESC	d3dsd;

	if (m_pSpriteSurface)
	{
		m_pSpriteSurface->Release();
		m_pSpriteSurface = 0;
	}

	if (m_pActiveSurface)
	{
		m_pActiveSurface->Release();
		m_pActiveSurface = 0;
	}
	
	NumSprites--;

	if (!pSourceSurface)
	{
		zen.SetError("Geen valide surface voor sprite");
		return E_FAIL;
	}

	
	m_pSpriteSurface = pSourceSurface;
	pSourceSurface->AddRef();

	m_SpriteWidth  = SpriteWidth;
	m_SpriteHeight = SpriteHeight;
	m_NumSprites   = NumSprites;
	
	m_pSpriteSurface->GetDesc(&d3dsd);
	m_SurfaceWidth  = d3dsd.Width;
	m_SurfaceHeight = d3dsd.Height;

	res = zen.m_pDevice->CreateImageSurface(m_SpriteWidth, m_SpriteHeight, D3DFMT_X8R8G8B8,
										&m_pActiveSurface);
	if (FAILED(res))
	{
		Shutdown();
		return E_FAIL;
	}

	RECT  SpriteRect  = {0, 0, SpriteWidth, SpriteHeight};
	POINT SpritePoint = {0, 0};

	zen.m_pDevice->CopyRects(m_pSpriteSurface, &SpriteRect, 1, m_pActiveSurface, &SpritePoint);

	m_bInitialized = TRUE;

	return S_OK;

}

HRESULT CSprite::SelectActiveSprite(int SpriteNum)
{
	HRESULT res;
	div_t	Result;
	int OffsetX = 0;
	int OffsetY = 0;
	int SpritesPerRow;

	SpritesPerRow = m_SurfaceWidth / m_SpriteWidth;

	if (SpriteNum > m_NumSprites)
		return E_FAIL;

	Result = div(SpriteNum, SpritesPerRow);

	OffsetX = Result.rem  * m_SpriteWidth;
	OffsetY = Result.quot * m_SpriteHeight;

	if (OffsetX > m_SurfaceWidth)
		return E_FAIL;

	if (OffsetY > m_SurfaceHeight)
		return E_FAIL;

	RECT  SpriteRect = {OffsetX, OffsetY, OffsetX + m_SpriteWidth, OffsetY + m_SpriteHeight};
	POINT DestPoint  = {0, 0};

	res = zen.m_pDevice->CopyRects(m_pSpriteSurface, &SpriteRect, 1, m_pActiveSurface, &DestPoint);
	if (FAILED(res))
		return E_FAIL;

	return S_OK;
}

HRESULT CSprite::Render(LPDIRECT3DSURFACE8 pDestSurface)
{
	HRESULT res;

	if (!pDestSurface)
		return E_FAIL;

	RECT  SourceRect = {0, 0, m_SpriteWidth, m_SpriteHeight};
	POINT DestPoint  = {m_x, m_y};

	if (m_bTransparent)
		res = zen.CopySurfaceToSurface(&SourceRect, m_pActiveSurface, &DestPoint, pDestSurface,
										TRUE, m_ColorKey);	
	else
		res = zen.m_pDevice->CopyRects(m_pActiveSurface, &SourceRect, 1, pDestSurface, &DestPoint);
		
	return res;
}

void CSprite::Shutdown()
{
	if (m_pSpriteSurface)
	{
		m_pSpriteSurface->Release();
		m_pSpriteSurface = 0;
	}

	if (m_pActiveSurface)
	{
		m_pActiveSurface->Release();
		m_pActiveSurface = 0;
	}

	m_SurfaceWidth	= 0;
	m_SurfaceHeight	= 0;
	m_SpriteWidth	= 0;
	m_SpriteHeight	= 0;
	m_NumSprites	= 0;

	m_pSpriteSurface	= 0;

	m_bInitialized		= FALSE;
}

D3DCOLOR CSprite::GetColorKey()
{
	return m_ColorKey;
}

void CSprite::SetColorKey(D3DCOLOR ColorKey)
{
}

void CSprite::SetPosition(int x, int y)
{
	m_x = x;
	m_y = y;
}

void CSprite::GetPosition(int * px, int * py)
{
	*px = m_x;
	*py = m_y;
}

void CSprite::GetBounds(RECT * pRect)
{
//	SetRect( pRect, m_x, m_y, m_SpriteWidth + m_x, m_SpriteHeight + m_y );
	SetRect( pRect, m_x, m_y, m_SpriteWidth, m_SpriteHeight );
}

void CSprite::SetVelocity(int Vx, int Vy)
{
	m_VelocityX = Vx;
	m_VelocityY = Vy;
}

void CSprite::GetVelocity(int * pVx, int * pVy)
{
	*pVx = m_VelocityX;
	*pVy = m_VelocityY;
}

void CSprite::SetNext(CSprite * pNext)
{
	m_pNext = pNext;
}

void CSprite::Update()
{
	m_x += m_VelocityX;
	m_y += m_VelocityY;
}

BOOL CSprite::CheckForCollisionWith(CSprite * pOtherSprite)
{
	return 0;
}

CSprite * CSprite::GetNext()
{
	return m_pNext;
}
